package seismosurfer.gui.menuitem;

import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;

import seismosurfer.gui.GUIConstants;
import seismosurfer.gui.panel.TopNPanel;

import com.bbn.openmap.MapHandler;
import com.bbn.openmap.gui.WindowSupport;
import com.bbn.openmap.gui.menu.MapHandlerMenuItem;

/**
 * This menu item shows the "Top-N Query"
 * dialog box and handles its events.
 *
 */
public class TopNMenuItem extends MapHandlerMenuItem implements GUIConstants {
    
    private static final long serialVersionUID = 8031133045972135146L;

    protected WindowSupport ws;

    protected TopNPanel topNPanel;

    private TopNMenuItemAction action = null;

    public TopNMenuItem() {
        super("Top-N Queries");

        setAction(getMenuItemAction("Top-N Queries"));
        topNPanel = new TopNPanel(getMenuItemAction());
    }
    
    /**
     * Creates an Action object (specifically, a TopNMenuItemAction)
     * that handles the events from the TopNPanel.
     * 
     * @param text the name of this Action
     * @return an Action instance
     */
    public Action getMenuItemAction(String text) {
        if (action == null) {
            action = new TopNMenuItemAction(text);
        }
        return action;
    }
    
    /**
     * Creates an Action object (specifically, a TopNMenuItemAction)
     * that handles the events from the TopNPanel. 
     * 
     * @return an Action instance
     */
    public Action getMenuItemAction() {
        return getMenuItemAction(null);
    }

    class TopNMenuItemAction extends AbstractAction {
        
        private static final long serialVersionUID = 2644564086129079404L;

        public TopNMenuItemAction(String text) {
            super(text);
        }

        public void actionPerformed(ActionEvent ae) {

            String command = ae.getActionCommand();

            if ((command.equals(OK)) && (ws != null)) {
                topNPanel.sendQueryParameters();
                ws.killWindow();
            } else if ((command.equals(CANCEL)) && (ws != null)) {
                ws.killWindow();
            } else {
                if (ws == null) {
                    ws = new WindowSupport(topNPanel, "Top-N Queries");
                }
                MapHandler mh = getMapHandler();
                Frame frame = null;
                if (mh != null) {
                    frame = (Frame) mh.get(java.awt.Frame.class);
                }
                ws.displayInWindow(frame);
                topNPanel.updateDialogValues();
            }
        }

    }

    public void findAndInit(Object someObj) {
        super.findAndInit(someObj);

        if (someObj instanceof MapHandler) {
            ((MapHandler) someObj).add(this);
            ((MapHandler) someObj).add(topNPanel);
        }

        topNPanel.findAndInit(someObj);
    }

    public void findAndUndo(Object someObj) {
        super.findAndUndo(someObj);
        topNPanel.findAndUndo(someObj);
    }
}
